<?php
//include_once("inc/Network.php");
include_once("inc/ParamManager.php");
include_once("inc/TaskManager.php");
include_once("inc/PcManager.php");
include_once("inc/ListManager.php");
//include_once("process.php");

//include_once("inc/header.php");

date_default_timezone_set("America/Denver");

//$tm = new TaskManager($network);

if(isset($_POST["cmd"])) {
	$cmd = $_POST["cmd"];
	$detail = $_POST["detail"];

	// Processing of commands occurs in a function, this allows for the
	// ability of a command to trigger other commands easily.
//	process($cmd, $detail);
}


// Load up PcManager for processing PCs on the bench
$mPc = new PcManager();

// Used with all commands which submit the page
//echo "<form name='main' method='post'>\n";
//echo "<input type='hidden' name='cmd'/>\n";
//echo "<input type='hidden' name='detail'/>\n";
//echo "</form>\n";

echo "<table><tr><td nowrap>\n";

// Display add pc
echo "<form name='command' method='post'>\n";
echo "Add PC: ";
echo "<input type='text' name='add_txt'/>\n";
echo "<input type='submit'"
		." onClick=\"javascript:process('cmd_wb_add', document.command.add_txt.value);\""
		." value='go'/>\n";
echo "</form>\n";

echo "<td nowrap>&nbsp;&nbsp;&nbsp;\n";
echo "<td nowrap>\n";

// Display dsquery
echo "<form name='dsquery' method='post'>\n";
echo "DsQuery: ";
echo "<input type='text' name='data'/>\n";
echo "<input type='submit'"
		." onClick=\"javascript:process('dsquery', document.dsquery.data.value);\""
		." value='go'/>\n";
//echo "(<a href='index.php'>refresh</a>)\n";
echo "</form>\n";

echo "</table>\n";

// table display
//echo "<table  id='box-table-a' cellspacing='0'>\n";
echo "<table>\n";
echo "<th>&nbsp;\n";
//echo "<th>ID\n";
echo "<th>Days\n";
echo "<th>Date\n";
echo "<th>Device\n";
echo "<th>UsedBy\n";
echo "<th>Status\n";
echo "<th>OS\n";
echo "<th>Model\n";
echo "<th>Memory\n";
echo "<th>Last Boot\n";
echo "<th>Installed\n";
echo "<th>Actions\n";
foreach($mPc as $pc) {
	$dt_today = time();
	// Calculate days_access, the number of days passed since pc was added to list
	$dt_access = strtotime($pc->dt_access);
	$days_access = floor(($dt_today - $dt_access) / 86400);
	// Calculate days_last_boot, the number of days since the pc was last rebooted
	$parts = explode(",", $pc->get("default", "System Boot Time"));
	$dt_last_boot = strtotime($parts[0]);
	if(strlen($dt_last_boot) != 0) {
		$days_last_boot = floor(($dt_today - $dt_last_boot) / 86400);
	}
	else {
		$days_last_boot = "";
	}
	// Calculate days_install, the number of days since the pc was installed
	$parts = explode(",", $pc->get("default", "Original Install Date"));
	$dt_install = strtotime($parts[0]);
	if(strlen($dt_install) != 0) {
		$days_install = floor(($dt_today - $dt_install) / 86400);
	}
	else {
		$days_install = "";
   	}
	// Form a short OS, from a longer OS description
	if(strpos($pc->get("default", "OS Name"), "Windows 7") !== false) {
		$os = "w7";
	}
	else if(strpos($pc->get("default", "OS Name"), "Windows XP") !== false) {
		$os = "xp";
	}
	else {
		$os = "?";
	}
	// Load up owners from qwinsta, this is for testing
	$lm = new ListManager($pc->id, "qwinsta");
	$owner = "";
	$first = true;
	foreach($lm as $key => $value) {
		if(!$first) { $owner .= ", "; } else { $first = false; }
		$owner .= "<a href='http://iisgenprod.micron.com/emp/Default.aspx?emp=$value' target='_blank'>$value</a>";
	}
	// Load up the status
	$status = $pc->get("default", "isPing");
	if(strlen($status) == 0) { $status = ""; }
	if($status == 1) {
		$status = "<font color='green'>Ok</font>";
	}
	else if($status == 0) {
		$status = "<del>Wmi</del>";
	}

	// table display
	echo "<tr>"
		."<td>"
			. "<input type='submit'"
				." onClick=\"javascript:process('del', '". $pc->name ."');\""
				." value='del'/>\n"
//		."<td align='center'>". $pc->id
		."<td align='center'>($days_access)"
		."<td nowrap>". date("Y-m-d", strtotime($pc->dt_access))
		."<td nowrap>". $pc->name
		."<td align='center' nowrap>[$owner]"	/* owner */
		."<td align='center' nowrap>$status"	/* status */
		."<td align='center' nowrap>$os"	/* os */
		."<td nowrap>"
				. $pc->get("default", "System Model")
		."<td nowrap>"
				. $pc->get("default", "Total Physical Memory")
		."<td align='center' nowrap>". "($days_last_boot days)"
//		."<td align='center' nowrap>". "(". $pc->get("default", "System Boot Time") .")"
		."<td align='center' nowrap>". "($days_install days)"
//		."<td align='center' nowrap>". "(". $pc->get("default", "Original Install Date") .")"
		."<td nowrap>"
			. "<input type='submit'"
				." onClick=\"javascript:process('sysinfo', '". $pc->name ."');\""
				." value='sysinfo'/>\n"
			. "<input type='submit'"
				." onClick=\"javascript:process('qwinsta', '". $pc->name ."');\""
				." value='qwinsta'/>\n"
			. "<input type='submit'"
				." onClick=\"javascript:process('ping', '". $pc->name ."');\""
				." value='ping'/>\n"
			. "<input type='submit'"
				." onClick=\"javascript:process('li_prod', '". $pc->name ."');\""
				." value='li_prod'/>\n"
			. "<input type='submit'"
				." onClick=\"javascript:process('analyze', '". $pc->name ."');\""
				." value='analyze'/>\n"
		;
}
echo "</table>\n";

//include_once("inc/footer.php");
?>
